<script type="text/javascript">
var enable_submit = true;
function submit_report_create() {
    var report_type_val = $("#schedule_create_modal").find("#report_type_val");
    var report_type = $("#schedule_create_modal").find("#report_type");
    var report_this_name = $("#schedule_create_modal").find("#report_this_name");
    var report_begin_time = $("#schedule_create_modal").find("#report_begin_time");
    var report_end_time = $("#schedule_create_modal").find("#report_end_time");;
    var report_week = $("#schedule_create_modal").find("#report_week");
    var report_week_val = $("#schedule_create_modal").find("#report_week_val");
    var bValid = true;
    var report_sum_week = '';
    var myDate = new Date();
    report_sum_week = report_week_val.val();
    if (report_week.attr("realvalue") == "hours") {
        report_sum_week = report_week_val.val() * 60;
    }
    if (report_week.attr("realvalue") == "days") {
        report_sum_week = report_week_val.val() * 1440;
    }


    var new_time = new Date()
    new_time.setMinutes(new_time.getMinutes());
    var report_end_time_a = new Date(report_end_time.val());
    
    if(new_time > report_end_time_a){
        
        $('.rtn-tip').text("结束时间应该大于目前系统时间").slideDown('fast');
        bValid = false;
    }
    
    //var dt=new Date();
    //dt.setTime(report_sum_week);
    //onsole.log(dt.toUTCString());
    
    bValid = bValid && checkRegexp(report_this_name, /^[0-9a-z-:]{1,32}$/, "名称必须由1至32位小写英文字母、数字、中划线或冒号构成");
    bValid = bValid && checkNull(report_begin_time, "请输入开始时间");
    bValid = bValid && checkNull(report_end_time, "请输入结束时间");
    bValid = bValid && checkNull(report_type, "未选择统计对象类型");
    bValid = bValid && checkNull(report_type_val, "统计对象名称不能为空");
    
    bValid = bValid && checkNull(report_week_val, "请输入周期");
    if(report_week.attr("realvalue") == "minutes"){
        bValid = bValid && checkRegexp(report_week_val, /^[0-9]*[1-9][0-9]*$/, "周期必须为正整数，并且是5的倍数");
        var week_report_val = report_sum_week / 5;
        bValid = bValid && checkdouble(report_week_val, week_report_val, /^[0-9a-z-]+$/, "周期必须为正整数，并且是5的倍数");
    }
    if(report_week.attr("realvalue") == "hours" || report_week.attr("realvalue") == "days"){
        bValid = bValid && checkRegexp(report_week_val, /^(\+?[1-9]\d*)$/, "必须为大于零的整数");
    }    
    if(report_end_time.val() < report_begin_time.val()){
        $('.rtn-tip').text("结束时间应该大于开始时间").slideDown('fast');
        bValid = false;
    }
    /*else{
        $('.rtn-tip').text("").slideDown('fast');
        bValid = true;
    }*/
    if (bValid) {
        if (enable_submit) {
            enable_submit = false;
            var $submitBtn = $('.modal-footer').find('.btn-blue');
            $submitBtn.html('<i class="fa fa-spinner fa-spin" style="margin:3px 6px;"></i>');
            $('.rtn-tip').slideUp('fast');
            var post_data = {
                "report_name": $.trim(report_this_name.val()),
                "start_time": $.trim(report_begin_time.val()),
                "end_time": $.trim(report_end_time.val()),
                "interval_time": $.trim(report_sum_week),
                "report_type": $.trim(report_type.attr('realvalue')),
                "type_id": $.trim(report_type_val.attr('realvalue'))
            };
            $.post("schedule/job/create", post_data,
                function(data) {
                    if (data.reply.is_success) {
                        angular.element('#report_create_button').scope().close();
                        $('#report_table').trigger("reloadGrid")
                    } else {
                        error = data.reply.error;
                        $('.rtn-tip').text(error).slideDown('fast');
                        $submitBtn.html(SelfTranslate("[[ 'label ok'|translate ]]"));
                        enable_submit = true;
                    }
                });
        }
    }
}

function doCalendar($el) {
    $el.calendar({
        setPos: function(targetObj, moveObj) {
            var coords = this.findPos(targetObj);
            moveObj.css({
                'position': 'fixed',
                'left': coords[0] + 'px',
                'top': (coords[1] + targetObj.offsetHeight) + 'px'
            });
        }
    });
}

function enable_click(text, type) {
    var obj_source = new Array();
    switch (type) {
        case 'host':
            {% for host in hosts %}
            var values = new Array();
            values[0] = "{{host.id}}";
            values[1] = "{{host.ip}}";
            obj_source.push(values); 
            {% endfor %}
            $('#report_type_val').rqDropDown({
                position: true,
                "datatype": "local",
                "source": obj_source
            });
            break;
        case 'cluster':
            {% for cluster in clusters %}
            var values = new Array();
            values[0] = "{{cluster.id}}";
            values[1] = "{{cluster.cluster_name}}";
            obj_source.push(values); 
            {% endfor %}
            $('#report_type_val').rqDropDown({
                position: true,
                "datatype": "local",
                "source": obj_source
            });
            break;
        case 'pool':
            {% for pool in pools %}
            var values = new Array();
            values[0] = "{{pool.id}}";
            values[1] = "{{pool.path}}";
            obj_source.push(values); 
            {% endfor %}
            $('#report_type_val').rqDropDown({
                position: true,
                "datatype": "local",
                "source": obj_source
            });
            break;
        case 'volume':
            {% for volume in volumes %}
            var values = new Array();
            values[0] = "{{volume.id}}";
            values[1] = "{{volume.path}}";
            obj_source.push(values);
            {% endfor %}
            $('#report_type_val').rqDropDown({
                position: true,
                "datatype": "local",
                "source": obj_source
            });
            break;
    }
}

$(function() {
    {% if super.p %}
        var type_source = [
        ['cluster', '集群'],
        ['host', '节点'],
        ['pool', '池'],
        ['volume', '卷']
    ];
        {% else %}
        var type_source = [
        ['pool', '池'],
        ['volume', '卷']
    ];
    {% endif %}
    
    $('#report_type').rqDropDown({
        position: true,
        "datatype": "local",
        "onchange": "enable_click",
        "source": type_source
    });

    var week_source = [
        ['minutes', '分钟'],
        ['hours', '小时'],
        ['days', '天']
    ];
    $('#report_week').rqDropDown({
        position: true,
        "datatype": "local",
        "source": week_source
    });

    var obj = {
        "report_this_name":"1至32位小写英文字母、数字、中划线或冒号构成。"
    }
    bindMouseEvent('#schedule_create_modal',obj);
});
</script>
<div class="modal-header">
    <h3>创建报表任务</h3>
</div>
<div class="modal-body modal-body-height" id="schedule_create_modal">
    <div class="volume-create-box">
    <div class="rtn-tip"></div>
        <table class="base-info">
            <tr>
                <td>
                    <label>名称</label><a>*</a>
                </td>
                <td colspan="3">
                    <input id="report_this_name" name="pool_mark_cr" size="25" max="10000" min="1" type="text" />
                    <i class="fa fa-question-circle" itype="report_this_name"></i>
                </td>
            </tr>
            <tr>
                <td>
                    <label>统计时间</label><a>*</a>
                </td>
                <td>
                    <input type="text" id="report_begin_time" maxlength="16" onfocus="doCalendar($(this))" readonly/>
                    <span class="unit fa fa-calendar"></span>
                </td>
                <td style="width: 12.5%;text-align: left;">
                    <label>至</label>
                </td>
                <td style="width: 37.5%;">
                    <input type="text" id="report_end_time" maxlength="16" onfocus="doCalendar($(this))" readonly/>
                    <span class="unit fa fa-calendar"></span>
                </td>
            </tr>
        </table>
        <table class="super-info">
            <tr>
                <td>
                    <label>对象类型</label><a>*</a>
                </td>
                <td>
                    <input id="report_type" readonly="readonly" class="rounded_dropdown" placeholder="请选择统计对象" realvalue="" />
                </td>
                <td>
                    <label>对象名称</label><a>*</a>
                </td>
                <td>
                    <input id="report_type_val" readonly="readonly" class="rounded_dropdown" placeholder="请选择">
                </td>
            </tr>
            <tr>
                <td>
                    <label>周期</label><a>*</a>
                </td>
                <td>
                    <input type="text" id="report_week_val" style="width: 50%;">
                    <input id="report_week" readonly="readonly" class="rounded_dropdown" value='分钟' realvalue="minutes" style="width: 45%;" />
                </td>
                <td>
                </td>
                <td>
                </td>
            </tr>
        </table>
    </div>
    <div class="error-tip">
        <p></p><span></span>
    </div>
    <div class="tip">
        <p></p><span></span>
    </div>
</div>
<div class="modal-footer">
    <button class="btn btn-blue" ng-click="ok()">[[ 'label ok'|translate ]]</button>
    <button class="btn btn-white" ng-click="cancel()">[[ 'label cancel'|translate ]]</button>
</div>
